import mysql.connector
from mysql.connector import Error
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk

# 全局变量
current_user = None
user_type = None
conn = None
cursor = None

def connect_to_database():
    global conn, cursor
    try:
        conn = mysql.connector.connect(
            host="localhost",
            user="root",
            password="Qb20160829",
            database="bar"
        )
        cursor = conn.cursor()
    except Error as e:
        messagebox.showerror("错误", f"连接数据库时出错: {e}")

def register_employee():
    id_val = entry_reg_id.get()
    name_val = entry_reg_name.get()
    phone_val = entry_reg_phone.get()
    position_val = entry_reg_position.get()
    password_val = entry_reg_password.get()

    try:
        cursor.execute("INSERT INTO Employee (ID, 姓名, 电话号码, 职务, 密码) VALUES (%s, %s, %s, %s, %s)",
                       (id_val, name_val, phone_val, position_val, password_val))
        conn.commit()
        messagebox.showinfo("成功", "员工注册成功")
        show_login_interface()
    except Error as e:
        messagebox.showerror("错误", f"注册员工时出错: {e}")

def register_customer():
    phone_val = entry_reg_customer_phone.get()
    name_val = entry_reg_customer_name.get()

    try:
        cursor.execute("INSERT INTO Customer (电话号码, 姓名) VALUES (%s, %s)",
                       (phone_val, name_val))
        conn.commit()
        messagebox.showinfo("成功", "顾客注册成功")
    except Error as e:
        messagebox.showerror("错误", f"注册顾客时出错: {e}")

def login():
    global current_user, user_type
    user_id = entry_login_id.get()
    password = entry_login_password.get()

    try:
        cursor.execute("SELECT * FROM Employee WHERE ID = %s AND 密码 = %s", (user_id, password))
        employee = cursor.fetchone()
        if employee:
            current_user = user_id
            user_type = "employee"
            show_employee_interface()
            return

        messagebox.showerror("错误", "用户不存在或密码错误")
    except Error as e:
        messagebox.showerror("错误", f"登录时出错: {e}")

def show_login_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

    tk.Label(frame, text="登录", font=("宋体", 24), bg="#f0f0f0").grid(row=0, columnspan=2, pady=20)
    tk.Label(frame, text="用户ID:", bg="#f0f0f0").grid(row=1, column=0, padx=10, pady=5)
    global entry_login_id
    entry_login_id = tk.Entry(frame)
    entry_login_id.grid(row=1, column=1, padx=10, pady=5)

    tk.Label(frame, text="密码:", bg="#f0f0f0").grid(row=2, column=0, padx=10, pady=5)
    global entry_login_password
    entry_login_password = tk.Entry(frame, show="*")
    entry_login_password.grid(row=2, column=1, padx=10, pady=5)

    tk.Button(frame, text="登录", command=login, bg="#7E0C6E", fg="white").grid(row=3, columnspan=2, pady=20)
    tk.Button(frame, text="注册员工", command=show_register_employee_interface, bg="#7E0C6E", fg="white").grid(row=4, columnspan=2, pady=10)

def show_register_employee_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

    tk.Label(frame, text="员工注册", font=("宋体", 24), bg="#f0f0f0").grid(row=0, columnspan=2, pady=20)

    tk.Label(frame, text="ID:", bg="#f0f0f0").grid(row=1, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_id
    entry_reg_id = tk.Entry(frame)
    entry_reg_id.grid(row=1, column=1, padx=10, pady=5, sticky='w')

    tk.Label(frame, text="姓名:", bg="#f0f0f0").grid(row=2, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_name
    entry_reg_name = tk.Entry(frame)
    entry_reg_name.grid(row=2, column=1, padx=10, pady=5, sticky='w')

    tk.Label(frame, text="电话:", bg="#f0f0f0").grid(row=3, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_phone
    entry_reg_phone = tk.Entry(frame)
    entry_reg_phone.grid(row=3, column=1, padx=10, pady=5, sticky='w')

    tk.Label(frame, text="职务:", bg="#f0f0f0").grid(row=4, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_position
    entry_reg_position = tk.Entry(frame)
    entry_reg_position.grid(row=4, column=1, padx=10, pady=5, sticky='w')

    tk.Label(frame, text="密码:", bg="#f0f0f0").grid(row=5, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_password
    entry_reg_password = tk.Entry(frame, show="*")
    entry_reg_password.grid(row=5, column=1, padx=10, pady=5, sticky='w')

    tk.Button(frame, text="注册", command=register_employee, bg="#4CAF50", fg="white").grid(row=6, columnspan=2, pady=20)
    tk.Button(frame, text="返回", command=show_login_interface, bg="#2196F3", fg="white").grid(row=7, columnspan=2, pady=10)

def show_employee_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.pack(fill='both', expand=True)

    tk.Label(frame, text="酒吧管理系统", font=("宋体", 24), bg="#f0f0f0").pack(pady=20)

    tk.Button(frame, text="查看酒水信息", command=show_beverages_interface, bg="#7E0C6E", fg="white").pack(pady=10)
    tk.Button(frame, text="查看所有订单", command=show_orders_interface, bg="#7E0C6E", fg="white").pack(pady=10)
    tk.Button(frame, text="管理酒水信息", command=manage_beverage_interface, bg="#7E0C6E", fg="white").pack(pady=10)
    tk.Button(frame, text="点单", command=place_order_interface, bg="#7E0C6E", fg="white").pack(pady=10)
    tk.Button(frame, text="注册顾客", command=show_register_customer_interface, bg="#7E0C6E", fg="white").pack(pady=10)
    tk.Button(frame, text="注销", command=show_login_interface, bg="#f44336", fg="white").pack(pady=10)

def show_beverages_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.pack(fill='both', expand=True)

    tk.Label(frame, text="酒水信息", font=("宋体", 40), bg="#f0f0f0").pack(pady=20)

    try:
        cursor.execute("SELECT * FROM Beverage")
        beverages = cursor.fetchall()
        tree = ttk.Treeview(frame, columns=("ID", "名称", "价格"), show="headings")
        tree.heading("ID", text="ID")
        tree.heading("名称", text="名称")
        tree.heading("价格", text="价格")
        for beverage in beverages:
            tree.insert("", tk.END, values=beverage)
        tree.pack(fill='both', expand=True, padx=10, pady=10)
    except Error as e:
        messagebox.showerror("错误", f"获取酒水信息时出错: {e}")

    tk.Button(frame, text="返回", command=show_employee_interface, bg="#2196F3", fg="white").pack(pady=10)

def show_orders_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.pack(fill='both', expand=True)

    tk.Label(frame, text="所有订单", font=("宋体", 24), bg="#f0f0f0").pack(pady=20)

    try:
        cursor.execute("SELECT * FROM OrderView")
        orders = cursor.fetchall()
        tree = ttk.Treeview(frame, columns=("订单时间", "酒水名称", "酒水价格", "顾客电话号", "顾客姓名"), show="headings")
        tree.heading("订单时间", text="订单时间")
        tree.heading("酒水名称", text="酒水名称")
        tree.heading("酒水价格", text="酒水价格")
        tree.heading("顾客电话号", text="顾客电话号")
        tree.heading("顾客姓名", text="顾客姓名")
        for order in orders:
            tree.insert("", tk.END, values=order)
        tree.pack(fill='both', expand=True, padx=10, pady=10)
    except Error as e:
        messagebox.showerror("错误", f"获取订单信息时出错: {e}")

    tk.Button(frame, text="返回", command=show_employee_interface, bg="#2196F3", fg="white").pack(pady=10)

def place_order_interface():
    global frame
    for widget in root.winfo_children():
        widget.destroy()

    outer_frame = tk.Frame(root, bg="#f0f0f0")
    outer_frame.pack(fill='both', expand=True)

    outer_frame.grid_columnconfigure(0, weight=1)
    outer_frame.grid_columnconfigure(1, weight=1)

    left_frame = tk.Frame(outer_frame, bg="#f0f0f0")
    left_frame.grid(row=0, column=0, sticky='e', padx=20, pady=20)

    right_frame = tk.Frame(outer_frame, bg="#f0f0f0")
    right_frame.grid(row=0, column=1, sticky='w', padx=20, pady=20)

    tk.Label(left_frame, text="点单", font=("宋体", 24), bg="#f0f0f0").grid(row=0, column=0, columnspan=2, pady=20)

    tk.Label(left_frame, text="顾客电话:", bg="#f0f0f0").grid(row=1, column=0, padx=10, pady=5, sticky='e')
    global entry_order_customer_phone
    entry_order_customer_phone = tk.Entry(left_frame)
    entry_order_customer_phone.grid(row=1, column=1, padx=10, pady=5, sticky='w')

    tk.Label(left_frame, text="酒水ID:", bg="#f0f0f0").grid(row=2, column=0, padx=10, pady=5, sticky='e')
    global entry_order_beverage_id
    entry_order_beverage_id = tk.Entry(left_frame)
    entry_order_beverage_id.grid(row=2, column=1, padx=10, pady=5, sticky='w')

    tk.Label(left_frame, text="数量:", bg="#f0f0f0").grid(row=3, column=0, padx=10, pady=5, sticky='e')
    global entry_order_quantity
    entry_order_quantity = tk.Entry(left_frame)
    entry_order_quantity.grid(row=3, column=1, padx=10, pady=5, sticky='w')

    tk.Button(left_frame, text="下单", command=place_order, bg="#4CAF50", fg="white").grid(row=4, column=0, columnspan=2, pady=20)
    tk.Button(left_frame, text="返回", command=show_employee_interface, bg="#2196F3", fg="white").grid(row=5, column=0, columnspan=2, pady=10)

    tk.Label(right_frame, text="酒水信息", font=("宋体", 18), bg="#f0f0f0").pack(pady=10)
    view_beverages_in_order_interface(right_frame)

def view_beverages_in_order_interface(parent):
    try:
        cursor.execute("SELECT * FROM Beverage")
        beverages = cursor.fetchall()
        tree = ttk.Treeview(parent, columns=("ID", "名称", "价格"), show="headings")
        tree.heading("ID", text="ID")
        tree.heading("名称", text="名称")
        tree.heading("价格", text="价格")
        for beverage in beverages:
            tree.insert("", tk.END, values=beverage)
        tree.pack(fill='both', expand=True, padx=10, pady=10)
    except Error as e:
        messagebox.showerror("错误", f"获取酒水信息时出错: {e}")

def place_order():
    customer_phone = entry_order_customer_phone.get()
    beverage_id = entry_order_beverage_id.get()
    quantity = int(entry_order_quantity.get())

    try:
        cursor.execute("SELECT 价格 FROM Beverage WHERE ID = %s", (beverage_id,))
        price = cursor.fetchone()[0]
        total_amount = price * quantity

        cursor.execute(
            "INSERT INTO Purchase (时间, 序列号, 金额, 顾客电话号码, 酒水ID) VALUES (NOW(), 0, %s, %s, %s)",
            (total_amount, customer_phone, beverage_id)
        )
        conn.commit()
        messagebox.showinfo("成功", "下单成功")
    except Error as e:
        messagebox.showerror("错误", f"下单时出错: {e}")

def show_register_customer_interface():
    for widget in root.winfo_children():
        widget.destroy()

    frame = tk.Frame(root, bg="#f0f0f0")
    frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

    tk.Label(frame, text="顾客注册", font=("宋体", 24), bg="#f0f0f0").grid(row=0, column=0, columnspan=2, pady=20)

    tk.Label(frame, text="电话:", bg="#f0f0f0").grid(row=1, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_customer_phone
    entry_reg_customer_phone = tk.Entry(frame)
    entry_reg_customer_phone.grid(row=1, column=1, padx=10, pady=5, sticky='w')

    tk.Label(frame, text="姓名:", bg="#f0f0f0").grid(row=2, column=0, padx=10, pady=5, sticky='e')
    global entry_reg_customer_name
    entry_reg_customer_name = tk.Entry(frame)
    entry_reg_customer_name.grid(row=2, column=1, padx=10, pady=5, sticky='w')

    tk.Button(frame, text="注册", command=register_customer, bg="#4CAF50", fg="white").grid(row=3, column=0, columnspan=2, pady=20)
    tk.Button(frame, text="返回", command=show_employee_interface, bg="#2196F3", fg="white").grid(row=4, column=0, columnspan=2, pady=10)

def manage_beverage_interface():
    global frame
    for widget in root.winfo_children():
        widget.destroy()

    outer_frame = tk.Frame(root, bg="#f0f0f0")
    outer_frame.pack(fill='both', expand=True)

    outer_frame.grid_columnconfigure(0, weight=1)
    outer_frame.grid_columnconfigure(1, weight=1)

    left_frame = tk.Frame(outer_frame, bg="#f0f0f0")
    left_frame.grid(row=0, column=0, sticky='e', padx=20, pady=20)

    right_frame = tk.Frame(outer_frame, bg="#f0f0f0")
    right_frame.grid(row=0, column=1, sticky='w', padx=20, pady=20)

    tk.Label(left_frame, text="管理酒水信息", font=("宋体", 24), bg="#f0f0f0").grid(row=0, column=0, columnspan=2, pady=20)

    tk.Label(left_frame, text="酒水ID:", bg="#f0f0f0").grid(row=1, column=0, padx=10, pady=5, sticky='e')
    global entry_beverage_id
    entry_beverage_id = tk.Entry(left_frame)
    entry_beverage_id.grid(row=1, column=1, padx=10, pady=5, sticky='w')

    tk.Label(left_frame, text="名称:", bg="#f0f0f0").grid(row=2, column=0, padx=10, pady=5, sticky='e')
    global entry_beverage_name
    entry_beverage_name = tk.Entry(left_frame)
    entry_beverage_name.grid(row=2, column=1, padx=10, pady=5, sticky='w')

    tk.Label(left_frame, text="价格:", bg="#f0f0f0").grid(row=3, column=0, padx=10, pady=5, sticky='e')
    global entry_beverage_price
    entry_beverage_price = tk.Entry(left_frame)
    entry_beverage_price.grid(row=3, column=1, padx=10, pady=5, sticky='w')

    tk.Button(left_frame, text="添加", command=add_beverage, bg="#4CAF50", fg="white").grid(row=4, column=0, columnspan=2, pady=10)
    tk.Button(left_frame, text="修改", command=update_beverage, bg="#2196F3", fg="white").grid(row=5, column=0, columnspan=2, pady=10)
    tk.Button(left_frame, text="删除", command=lambda: delete_beverage(entry_beverage_id.get()), bg="#f44336", fg="white").grid(row=6, column=0, columnspan=2, pady=10)
    tk.Button(left_frame, text="返回", command=show_employee_interface, bg="#2196F3", fg="white").grid(row=7, column=0, columnspan=2, pady=10)

    tk.Label(right_frame, text="酒水信息", font=("宋体", 18), bg="#f0f0f0").pack(pady=10)
    view_beverages_in_manage_interface(right_frame)

def view_beverages_in_manage_interface(parent):
    try:
        cursor.execute("SELECT * FROM Beverage")
        beverages = cursor.fetchall()
        tree = ttk.Treeview(parent, columns=("ID", "名称", "价格"), show="headings")
        tree.heading("ID", text="ID")
        tree.heading("名称", text="名称")
        tree.heading("价格", text="价格")
        for beverage in beverages:
            tree.insert("", tk.END, values=beverage)
        tree.pack(fill='both', expand=True, padx=10, pady=10)
    except Error as e:
        messagebox.showerror("错误", f"获取酒水信息时出错: {e}")

def add_beverage():
    beverage_id = entry_beverage_id.get()
    name = entry_beverage_name.get()
    price = float(entry_beverage_price.get())

    try:
        cursor.execute("INSERT INTO Beverage (ID, 名称, 价格) VALUES (%s, %s, %s)", (beverage_id, name, price))
        conn.commit()
        messagebox.showinfo("成功", "酒水添加成功")
    except Error as e:
        messagebox.showerror("错误", f"添加酒水时出错: {e}")

def update_beverage():
    beverage_id = entry_beverage_id.get()
    price_str = entry_beverage_price.get()

    if not price_str:
        messagebox.showerror("错误", "价格不能为空")
        return

    try:
        cursor.callproc('update_beverage_price', (beverage_id, float(price_str)))
        conn.commit()
        messagebox.showinfo("成功", "酒水信息修改成功")
    except Error as e:
        if "酒水ID不存在" in str(e):
            messagebox.showerror("错误", "酒水ID不存在")
        else:
            messagebox.showerror("错误", f"修改酒水信息时出错: {e}")


def delete_beverage(beverage_id):
    global conn, cursor
    try:
        conn.commit()
        conn.start_transaction()

        delete_purchase_query = "DELETE FROM Purchase WHERE 酒水ID = %s"
        cursor.execute(delete_purchase_query, (beverage_id,))
        delete_beverage_query = "DELETE FROM Beverage WHERE ID = %s"
        cursor.execute(delete_beverage_query, (beverage_id,))

        conn.commit()
        messagebox.showinfo("成功", "删除操作成功并提交事务。")

    except Error as e:
        messagebox.showerror("错误", f"删除操作时出错: {e}")
        conn.rollback()

root = tk.Tk()
root.title("酒吧管理系统")
root.state('zoomed')
root.configure(bg="#f0f0f0")

connect_to_database()
show_login_interface()

root.mainloop()
